package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysPositionsAvailable;
import com.ruoyi.system.domain.SysPositionsApply;
import com.ruoyi.system.service.ISysPositionsAvailableService;
import com.ruoyi.system.service.ISysPositionsApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 地图数据统计 Controller
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/system/map")
public class MapStatisticsController extends BaseController {

    @Autowired
    private ISysPositionsAvailableService positionsAvailableService;

    @Autowired
    private ISysPositionsApplyService positionsApplyService;

    /**
     * 获取地图统计数据
     */
    @PreAuthorize("@ss.hasPermi('system:map:query')")
    @GetMapping("/statistics")
    public AjaxResult getMapStatistics() {
        Map<String, Object> result = new HashMap<>();

        // 获取所有岗位数据
        List<SysPositionsAvailable> jobs = positionsAvailableService.selectSysPositionsAvailableList(new SysPositionsAvailable());

        // 获取所有报名数据
        List<SysPositionsApply> applies = positionsApplyService.selectSysPositionsApplyList(new SysPositionsApply());

        result.put("jobData", jobs);
        result.put("applyData", applies);

        return AjaxResult.success(result);
    }

    /**
     * 获取岗位分布数据
     */
    @PreAuthorize("@ss.hasPermi('system:map:query')")
    @GetMapping("/job-distribution")
    public AjaxResult getJobDistribution() {
        List<SysPositionsAvailable> jobs = positionsAvailableService.selectSysPositionsAvailableList(new SysPositionsAvailable());

        // 按地点统计岗位数据
        Map<String, Map<String, Object>> locationStats = new HashMap<>();

        for (SysPositionsAvailable job : jobs) {
            String location = job.getLocation() != null ? job.getLocation() : "未知";

            System.out.println(location);

            if (!locationStats.containsKey(location)) {
                Map<String, Object> stats = new HashMap<>();
                stats.put("totalJobs", 0);
                stats.put("technicalJobs", 0);
                stats.put("generalJobs", 0);
                stats.put("managementJobs", 0);
                locationStats.put(location, stats);
            }

            Map<String, Object> stats = locationStats.get(location);
            stats.put("totalJobs", (Integer) stats.get("totalJobs") + (job.getPositionNumber() != null ? job.getPositionNumber() : 0));

            // 根据岗位性质分类
            String nature = job.getPositionNature() != null ? job.getPositionNature() : "";
            if (nature.contains("技术")) {
                stats.put("technicalJobs", (Integer) stats.get("technicalJobs") + (job.getPositionNumber() != null ? job.getPositionNumber() : 0));
            } else if (nature.contains("普通")) {
                stats.put("generalJobs", (Integer) stats.get("generalJobs") + (job.getPositionNumber() != null ? job.getPositionNumber() : 0));
            } else if (nature.contains("管理")) {
                stats.put("managementJobs", (Integer) stats.get("managementJobs") + (job.getPositionNumber() != null ? job.getPositionNumber() : 0));
            }
        }

        return AjaxResult.success(locationStats);
    }

    /**
     * 获取报名分布数据
     */
    @PreAuthorize("@ss.hasPermi('system:map:query')")
    @GetMapping("/apply-distribution")
    public AjaxResult getApplyDistribution() {
        List<SysPositionsApply> applies = positionsApplyService.selectSysPositionsApplyList(new SysPositionsApply());

        // 按地点统计报名数据
        Map<String, Map<String, Object>> locationStats = new HashMap<>();

        for (SysPositionsApply apply : applies) {
            String location = apply.getLocation() != null ? apply.getLocation() : "未知";

            if (!locationStats.containsKey(location)) {
                Map<String, Object> stats = new HashMap<>();
                stats.put("totalApplies", 0);
                stats.put("technicalApplies", 0);
                stats.put("generalApplies", 0);
                stats.put("managementApplies", 0);
                locationStats.put(location, stats);
            }

            Map<String, Object> stats = locationStats.get(location);
            stats.put("totalApplies", (Integer) stats.get("totalApplies") + 1);

            // 根据岗位性质分类
            String nature = apply.getLocation() != null ? apply.getLocation() : "";
            if (nature.contains("技术")) {
                stats.put("technicalApplies", (Integer) stats.get("technicalApplies") + 1);
            } else if (nature.contains("普通")) {
                stats.put("generalApplies", (Integer) stats.get("generalApplies") + 1);
            } else if (nature.contains("管理")) {
                stats.put("managementApplies", (Integer) stats.get("managementApplies") + 1);
            }
        }

        return AjaxResult.success(locationStats);
    }
}
